#include "tools.h"

int countLinesInFile(const QString &filePath)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "无法打开文件：" << filePath;
        return 0;
    }

    int lineCount = 0;
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        if (!line.trimmed().isEmpty()) {
            ++lineCount;
        }
    }

    file.close();
    return lineCount;
}



int countIncompleteLines(const QString &filePath)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "无法打开文件:" << filePath;
        return -1;
    }

    QTextStream in(&file);
    in.setCodec("UTF-8");

    int incompleteCount = 0;
    bool isFirstLine = true;

    while (!in.atEnd()) {
        QString line = in.readLine().trimmed();

        // 跳过空行
        if (line.isEmpty())
            continue;

        // 跳过第一行表头
        if (isFirstLine) {
            isFirstLine = false;
            continue;
        }

        // 使用 '\t' 作为分隔符，拆分字段
        QStringList fields = line.split('\t');

        // 检查任意字段是否为 "-"
        bool hasIncompleteField = std::any_of(fields.begin(), fields.end(), [](const QString &field) {
            return field.trimmed() == "-";
        });

        if (hasIncompleteField)
            ++incompleteCount;
    }

    file.close();
    return incompleteCount;
}
